package com.itheima.exer.homework.hw1008;

import java.io.*;

public class IOTest2 {
    private static final File FILE = new File("E:\\z实验文件夹\\a\\vs.txt");
    private static final File FILE1 = new File("E:\\z实验文件夹\\a\\aaa123.txt");

    public static void main(String[] args) throws Exception {
        //使用字节输入输出流一次读取一个字节的方式复制文件;
        //long time = byteStreamWithOneByte();

        //使用字节输入输出流一次读取一个数组的方式复制文件;
        //long time = byteStreamWithOneArray();


        //使用字节缓冲输入输出流一次读取一个字节的方式复制文件;
        //long time = BufferedStreamWithOneByte();

        //使用字节缓冲输入输出流一次读取一个数组的方式复制文件;
        long time = BufferedStreamWithOneArray();

        System.out.println(time);
    }

    private static long BufferedStreamWithOneArray() throws Exception {
        long time1 = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(FILE1));
        byte[] bytes = new byte[102400];
        int len;
        while ((len = bis.read()) != -1){
            bos.write(bytes);
        }
        bis.close();
        bos.close();
        long time2 = System.currentTimeMillis();
        return time2 - time1;
    }

    private static long BufferedStreamWithOneByte() throws IOException {
        long time1 = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(FILE1));
        int b;
        while ((b = bis.read()) != -1){
            bos.write(b);
        }
        bis.close();
        bos.close();
        long time2 = System.currentTimeMillis();
        return time2 - time1;
    }

    private static long byteStreamWithOneArray() throws Exception {
        long time1 = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(FILE);
        FileOutputStream fos = new FileOutputStream(FILE1);
        byte[] bytes = new byte[102400];
        int len;
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes);
        }
        fis.close();
        fos.close();
        long time2 = System.currentTimeMillis();
        return time2 - time1;
    }

    private static long byteStreamWithOneByte() throws IOException {
        long time1 = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(FILE);
        FileOutputStream fos = new FileOutputStream(FILE1);
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fis.close();
        fos.close();
        long time2 = System.currentTimeMillis();
        return time2 - time1;
    }
}
